package logic

import (
	"context"
	"order/internal/model"

	"order/internal/svc"
	"order/order"

	"github.com/zeromicro/go-zero/core/logx"
)

type OrderPageLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewOrderPageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OrderPageLogic {
	return &OrderPageLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *OrderPageLogic) OrderPage(in *order.OrderPageRequest) (*order.OrderPageResponse, error) {
	orderModel := model.NewOrderModel(l.ctx, l.svcCtx)
	orderCreated, err := orderModel.OrderCreated(int(in.Page), int(in.PageSize))
	if err != nil {
		return nil, err
	}

	var orderInfo []*order.OrderInfo

	for _, v := range orderCreated {
		orderInfo = append(orderInfo, &order.OrderInfo{
			OrderNo:    v.OrderNo,
			UserId:     int64(v.UserId),
			GoodsId:    int64(v.GoodsId),
			OrderNum:   int64(v.OrderNum),
			OrderPrice: float32(v.OrderPrice),
			CreatedAt:  v.CreatedAt,
		})
	}

	return &order.OrderPageResponse{Page: orderInfo}, nil
}
